# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=ncurses
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_base_ver=6.4
_date_rev=20230708
pkgver=${_base_ver}.${_date_rev}
pkgrel=1
pkgdesc="System V Release 4.0 curses emulation library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
depends=("${MINGW_PACKAGE_PREFIX}-libsystre")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
url="https://www.gnu.org/software/ncurses/"
license=('spdx:MIT')
options=('staticlibs' 'strip')
# https://ftp.gnu.org/pub/gnu/ncurses/ncurses-${pkgver}.tar.gz{,.sig}
source=("https://invisible-mirror.net/archives/ncurses/current/${_realname}-${_base_ver}-${_date_rev}.tgz"{,.asc}
        002-ncurses-config-win-paths.patch
        ncurses-6.3-cflags-private.patch
        ncurses-6.3-pkgconfig.patch)
sha256sums=('aa8f8e49b02643d759bf154142f5e766fb1eaa5e41780a5e8fdaaf845e69e47a'
            'SKIP'
            '5367d8f49aff92884b9daa014502df13e1812f1b7ee1b3a3cb18139f10039408'
            '3107029dfb807e338d34641d78329cd6725c58e6b873352621f4b9611a8380bf'
            'b8544a607dfbeffaba2b087f03b57ed1fa81286afca25df65f61b04b5f3b3738')
validpgpkeys=('19882D92DDA4C400C22C0D56CC2AF4472167BE03')  # "Thomas E. Dickey (self-signed w/o SHA1) <dickey@invisible-island.net>"

prepare() {
  cd ${_realname}-${_base_ver}-${_date_rev}
  patch -p1 -i ${srcdir}/002-ncurses-config-win-paths.patch

  # do not leak build-time LDFLAGS into the pkgconfig files:
  # https://bugs.archlinux.org/task/68523
  patch -p1 -i ${srcdir}/ncurses-6.3-pkgconfig.patch

  patch -p1 -i ${srcdir}/ncurses-6.3-cflags-private.patch
}

build() {
  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  # It passes X_OK to access() on Windows which isn't supported with ucrt
  CFLAGS+=" -D__USE_MINGW_ACCESS"
  # nanosleep is only defined in pthread library
  export cf_cv_func_nanosleep=no

  ../${_realname}-${_base_ver}-${_date_rev}/configure \
    --prefix=${MINGW_PREFIX} \
    --mandir=${MINGW_PREFIX}/share/man \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --without-ada \
    --with-cxx \
    --with-shared \
    --without-pthread \
    --disable-rpath \
    --enable-colorfgbg \
    --enable-ext-colors \
    --enable-ext-mouse \
    --disable-symlinks \
    --enable-warnings \
    --enable-assertions \
    --disable-home-terminfo \
    --enable-database \
    --enable-sp-funcs \
    --enable-term-driver \
    --enable-interop \
    --enable-widec \
    --enable-pc-files \
    --with-pkg-config-libdir=${MINGW_PREFIX}/lib/pkgconfig

  make
}

package() {
  cd ${srcdir}/build-${MSYSTEM}
  make DESTDIR=${pkgdir} install
  cp -r ${pkgdir}${MINGW_PREFIX}/include/ncursesw ${pkgdir}${MINGW_PREFIX}/include/ncurses
  cp ${pkgdir}${MINGW_PREFIX}/lib/libncursesw.a ${pkgdir}${MINGW_PREFIX}/lib/libncurses.a

  # Use predefined variables in pkgconfig files (#13958)
  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${MINGW_PREFIX}/include|\${includedir}|g" -i "${pcfile}"
    sed -s "s|${MINGW_PREFIX}/lib|\${libdir}|g" -i "${pcfile}"
  done
}
